/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.Component;
import org.aswing.geom.Dimension;
import org.aswing.geom.Rectangle;
import org.aswing.resizer.ResizeStrategy;

/**
 * Resizing behaviour with key control.
 * 
 * <p>SHIFT Key : For proportionnal resizing</li>
 * 
 * <p>Example
 * {@code
 *   var p : JPanel = new JPanel();
 *   p.setSize( 200, 100 );
 *   p.setLocation(20,20);
 *   p.setOpaque(true);
 *   p.setBackground( ASColor.DARK_GRAY );
 *   
 *   var c : ResizerController = ResizerController.init( p );
 *   c.setResizer( FvResizerFactory.create( new FvResizeEngine() ) );
 *   
 *   var pane : Container = FvAsWing.getInstance().getContainer();
 *   pane.setLayout( new EmptyLayout() );
 *   pane.append( p );
 * }
 * 
 * @see fvaswing.resizer.FvResizerFactory 
 * 
 * @author Romain Ecarnot
 */
class fvaswing.resizer.FvResizeEngine implements ResizeStrategy
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
		
	private var _wSign : Number;
	private var _hSign : Number;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * @param wSign Horizontal resizing direction
	 * @param hSign Vertical resizing direction
	 */
	public function FvResizeEngine( wSign : Number, hSign : Number )
	{
		_wSign = wSign;
		_hSign = hSign;
	}
	
	/**
	 * Count and return the new bounds what the component would be resized to.<br>
	 * 
 	 * <p>It will return the resized rectangle, the rectangle is not min than 
 	 * getResizableMinSize and not max than getResizableMaxSize if these method are defineded
 	 * in the resized comopoent.
	 */
	public function getBounds( com : Component, movedX : Number, movedY : Number ) : Rectangle
	{
		var currentBounds : Rectangle = com.getBounds();
		var minSize : Dimension = com.getMinimumSize();
		var maxSize : Dimension = com.getMaximumSize();
		
		var ratio : Number = currentBounds.width / currentBounds.height;
		var proportional : Boolean = _checkPropportionality( movedX, movedY ); 
		
		var maxBounds : Rectangle = com.getParent().getBounds();
    	maxBounds.x += 10;
    	maxBounds.y += 10;
    	maxBounds.width -= 10;
    	maxBounds.height -= 10;
    	
		if( minSize == undefined ) minSize = new Dimension(0, 0);
		
		if( maxSize == undefined )
			maxSize = new Dimension( Number.MAX_VALUE, Number.MAX_VALUE );
		
		var newX : Number;
		var newY : Number;
		var newW : Number;
		var newH : Number;
		
		if( _wSign == 0 ) newW = currentBounds.width;
		else
		{
			newW = currentBounds.width + ( _wSign*movedX );
			newW = Math.min( maxSize.width, Math.max(minSize.width, newW) );
		}
		
		if( _wSign < 0 ) newX = currentBounds.x + ( currentBounds.width - newW );
		else newX = currentBounds.x;
		
		if( proportional ) newH = newW / ratio;
		else
		{
			if( _hSign == 0 ) newH = currentBounds.height;
			else
			{
				newH = currentBounds.height + ( _hSign*movedY );
				newH = Math.min( maxSize.height, Math.max(minSize.height, newH ) );
			}
		}	
		
		if( _hSign < 0 )
			newY = currentBounds.y + ( currentBounds.height - newH );
		else newY = currentBounds.y;
    	
		return new Rectangle( newX, newY, newW, newH );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
		
	private function _checkPropportionality( movedX : Number, movedY : Number ) : Boolean
	{
		 var isCall : Boolean = Key.isDown( Key.SHIFT );
		 var isAvailable : Boolean = ( Boolean(movedX) && Boolean(movedY) );
		 return ( isCall && isAvailable );
	}
}
